package com.qq.first_project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qq.first_project.entity.Blog;
import com.qq.first_project.entity.Comment;
import com.qq.first_project.entity.vo.CommentVo;
import com.qq.first_project.mapper.BlogMapper;
import com.qq.first_project.mapper.CommentMapper;
import com.qq.first_project.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Qiu
 * @since 2023-01-30
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    @Resource
    private CommentMapper commentMapper;

    @Autowired
    @Resource
    private BlogMapper blogMapper;

    //存放迭代找出的所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();

    /**
     * @Description: 查询评论
     * @Date: 17:26 2020/4/14
     * @Param:
     * @Return: 评论消息
     */
    @Override
    public List<Comment> listComment(Long blogId) {
        //查询出父节点
        List<Comment> comments = commentMapper.findByParentIdNull(Long.parseLong("-1"),blogId);
        for(Comment comment : comments){
            Long id = comment.getId();
            String parentNickname1 = comment.getNickname();
            List<Comment> childComments = commentMapper.findByParentIdNotNull(id,blogId);
            //查询出子评论
            combineChildren(childComments, parentNickname1,blogId);
            comment.setReplyComments(tempReplys);
            tempReplys = new ArrayList<>();
        }
        return comments;
    }

    /**
     * @Description: 查询出子评论
     * @Auther: ONESTAR
     * @Date: 17:31 2020/4/14
     * @Param: childComments：所有子评论
     * @Param: parentNickname1：父评论的姓名
     * @Return:
     */
    private void combineChildren(List<Comment> childComments, String parentNickname1,Long blogId) {
        //判断是否有一级子回复
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(Comment childComment : childComments){
                String parentNickname = childComment.getNickname();
                childComment.setParentNickname(parentNickname1);
                tempReplys.add(childComment);
                Long childId = childComment.getId();
                //查询二级以及所有子集回复
                recursively(childId, parentNickname,blogId);
            }
        }
    }

    /**
     * @Description: 循环迭代找出子集回复
     * @Auther: ONESTAR
     * @Date: 17:33 2020/4/14
     * @Param: childId：子评论的id
     * @Param: parentNickname1：子评论的姓名
     * @Return:
     */
    private void recursively(Long childId, String parentNickname1,Long blogId) {
        //根据子一级评论的id找到子二级评论
        List<Comment> replayComments = commentMapper.findByReplayId(childId,blogId);

        if(replayComments.size() > 0){
            for(Comment replayComment : replayComments){
                String parentNickname = replayComment.getNickname();
                replayComment.setParentNickname(parentNickname1);
                Long replayId = replayComment.getId();
                tempReplys.add(replayComment);
                //循环迭代找出子集回复
                recursively(replayId,parentNickname,blogId);
            }
        }
    }

    @Override
    //存储评论信息
    public int saveComment(Comment comment) {
        // 如果增加的是父评论
        if (comment.getParentCommentId() == Long.parseLong("-1")){
            //查询出该博客的父评论
            List<Comment> comments = commentMapper.findByParentIdNull(Long.parseLong("-1"),comment.getBlogId());
            //文章评论数加1
            blogMapper.updateBlogCommentCount(comment.getBlogId(),comments.size()+1);
        }
        return commentMapper.saveComment(comment);
    }

    /**
     * 查询所有评论
     * @return
     */
    @Override
    public IPage<CommentVo> listAll(Page<CommentVo> page, String name) {
        return commentMapper.listAll(page,name);
    }

    /**
     * 更新评论
     * @param commentVo
     * @return
     */
    @Override
    public int updateCommentById(CommentVo commentVo) {
        return commentMapper.updateCommentById(commentVo);
    }
}
